<?php
class PackageController extends Core_Controller
{
    public function indexAction()
    {
        if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Quản lý gói dịch vụ";
        $this->_view->activeClass = "service";
        
        $modelPackage = new Admin_Model_Package();
        $this->_view->list = $modelPackage->getPackages();
    }
    
    public function createAction()
    {
        
        if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Thêm gói dịch vụ";
        $this->_view->activeClass = "service";
        
        //status  0: chua submit , 1: da submit va co loi , 2: submit thanh cong
        $status = 0;
        $message = '';
        $pn = '';
        $cpu = '';
        $ram = '';
        $os  = '';
        $pph = '';
        $ti  = '';
        
        if(isset($_POST['package_name']))
        {
            $pn = trim($_POST['package_name']);
            $cpu = trim($_POST['cpu']);
            $ram = trim($_POST['ram']);
            $os = trim($_POST['osname']);
            $pph = trim($_POST['pph']);
            $ti = trim($_POST['template_id']);
            $status = 1;
            
            if(($pn == "") || ($cpu == "") || ($ram == "") || ($os == "") || ($pph == "") || ($ti == ""))
            {
               $message = " Chưa nhập đủ thông tin";    
            } 
             else
                {
                    $modelPackage = new Admin_Model_Package();
                    $ok = $modelPackage->insertPackage($pn,$cpu,$ram,$os,$ti,$pph);
                    if(! $ok )
                       $message = " Thêm gói dịch vụ thất bại do lỗi hệ thống";
                    else
                    {
                        $status = 2;
                        $this->redirect('/admin/package');
                    }  
                }
        }
        
        $this->_view->status = $status;
        $this->_view->message = $message;
        $this->_view->input_package_name = $pn;
        $this->_view->input_cpu = $cpu;
        $this->_view->input_ram = $ram;
        $this->_view->input_osname = $os;
        $this->_view->input_pph = $pph;
        $this->_view->input_template_id = $ti;
        
        $modelTemp = new Admin_Model_Template();
        $this->_view->listTemp = $modelTemp->getTemplates();
    }
    
    public function editAction()
    {
        if(count($this->_http->getArvs()) != 1 ) throw new Exception("404 not found",404);
        $id = $this->_http->getParam('id');
        if($id == null) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Sửa gói dịch vụ";
        $this->_view->activeClass = "service";
        
        //status  0: chua submit , 1: da submit va co loi , 2: submit thanh cong
        $status = 0;
        $message = '';
        $pn = '';
        $cpu = '';
        $ram = '';
        $os  = '';
        $pph = '';
        $ti  = '';
        
        if(isset($_POST['package_name']))
        {
            $pn = trim($_POST['package_name']);
            $cpu = trim($_POST['cpu']);
            $ram = trim($_POST['ram']);
            $os = trim($_POST['osname']);
            $pph = trim($_POST['pph']);
            $ti = trim($_POST['template_id']);
            $status = 1;
            
            if(($pn == "") || ($cpu == "") || ($ram == "") || ($os == "") || ($pph == "") || ($ti == ""))
            {
               $message = " Chưa nhập đủ thông tin";    
            } 
             else
                {
                    $modelPackage = new Admin_Model_Package();
                    $ok = $modelPackage->updatePackage($id,$pn,$cpu,$ram,$os,$ti,$pph);
                    if(! $ok )
                       $message = " Sửa gói dịch vụ thất bại do lỗi hệ thống";
                    else
                    {
                        $status = 2;
                        $this->redirect('/admin/package');
                    }  
                }
        }
        else
        {
            $modelPackage = new Admin_Model_Package();
            $result = $modelPackage->getPackage($id);
            if($result == null) throw new Exception("404 not found",404);
            $pn = $result[0]['package_name'];
            $cpu = $result[0]['cpu'];
            $ram = $result[0]['ram'];
            $os = $result[0]['os_name'];
            $pph = $result[0]['pph'];
            $ti = $result[0]['template_id'];
        }
        
        $this->_view->status = $status;
        $this->_view->message = $message;
        $this->_view->input_package_name = $pn;
        $this->_view->input_cpu = $cpu;
        $this->_view->input_ram = $ram;
        $this->_view->input_osname = $os;
        $this->_view->input_pph = $pph;
        $this->_view->input_template_id = $ti;
        
        $modelTemp = new Admin_Model_Template();
        $this->_view->listTemp = $modelTemp->getTemplates();
        $this->_view->id = $id; 
    }
    
    public function deleteAction()
    {
        if(count($this->_http->getArvs()) != 1 ) throw new Exception("404 not found",404);
        $id = $this->_http->getParam('id');
        if($id == null) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        
        //xoa cac may ao su dung package nay
        $modelVm = new Model_Vm();
        $vm_info = $modelVm->getVms_Package($id);
   	    if($vm_info != null)
	    {
	        	$countVms = count($vm_info);
	        	$apiVm    = new Api_Vm();
	        	for($k = 0;$k < $countVms; $k++)
	        	{
	        		if($vm_info[$k]['status'] == 1)
	        			$apiVm->stopVm($vm_info[$k]['default_name']);
	           		$apiVm->deleteVm($vm_info[$k]['username'],$vm_info[$k]['vmname']);
	           		$modelVm->deleteVm($vm_info[$k]['vm_id']);
	        	}
	     }
        
        $modelPackage = new Admin_Model_Package();
        $ok = $modelPackage->deletePackage($id);
        $this->redirect('/admin/package');
    }
}